package com.zhongxiangspace.controller.admin;

import com.zhongxiangspace.entity.*;
import com.zhongxiangspace.entity.VO.BannerVO;
import com.zhongxiangspace.entity.VO.TradeAreaVO;
import com.zhongxiangspace.result.ExceptionResult;
import com.zhongxiangspace.result.NormalResult;
import com.zhongxiangspace.result.Result;
import com.zhongxiangspace.service.*;
import com.zhongxiangspace.tools.FreeMarkerViewUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 楼盘详情页
 */
@Controller
public class HousDetailMobileHtmlCreateController {
    @Autowired
    private CountyService countyService;
    @Autowired
    private OfficeBuildingService officeBuildingService;
    @Autowired
    private HouseService houseService;
    @Autowired
    private ArticleService articleService;
    @Autowired
    private AdvertService advertService;

    /**
     * 房源详情页
     *
     * @param request
     * @param pageType
     * @return
     */
    @ResponseBody
    @PostMapping("/htmlCreate/html/mobileHousedetails.do")
    public Result ajxaCreateHouse(HttpServletRequest request, String pageType) {
        try {
            QueryAnalyzer queryAnalyzer = new QueryAnalyzer();
            List<Map<String, Object>> maps = this.houseService.queryAll(queryAnalyzer);
            for (int i = 0; i < maps.size(); i++) {
                this.ajxaCreatHtmlTools((Integer) maps.get(i).get("houes_id"));
            }
            return new NormalResult(true);
        } catch (Exception e) {
            e.printStackTrace();
            return new ExceptionResult(e);
        }
    }

    public boolean ajxaCreatHtmlTools(Integer id) {
        try {
            FreemarkerHtmlConfiguration freemarkerHtmlConfiguration = new FreemarkerHtmlConfiguration();
            freemarkerHtmlConfiguration.setTemplateName("office_hous_detail.ftl");
            freemarkerHtmlConfiguration.setHtmlPath("/html/wap/details/h"+id+".html");
            freemarkerHtmlConfiguration.setTemplatePath("html/mobile");
            Map<String, Object> map = new HashMap<>();
            House house = this.houseService.queryOne(id);
            map.put("house", house);
            //获取楼盘下面的有多少套房源
            Integer sumHouse=this.houseService.querySumHouse(house.getBuilding_id());
            map.put("sumHouse",sumHouse);
            BuildingsAttribute buildingsAttribute = this.officeBuildingService.queryAttr(house.getBuilding_id());
            map.put("buildingsAttribute", buildingsAttribute);
            County county = this.countyService.queryOne(house.getCounty_id());
            map.put("county", county);
            County trade_area = this.countyService.queryOne(house.getTrade_area_id());
            map.put("trade_area", trade_area);
            OfficeBuildings office = this.officeBuildingService.queryOne(house.getBuilding_id());
            map.put("office", office);
            //热门楼盘
            QueryAnalyzer buildingQueryAnalyzer = new QueryAnalyzer();
            buildingQueryAnalyzer.setShowCode(1);
            buildingQueryAnalyzer.setHotCode(1);
            List<Map<String, Object>> officeBuildingsList = this.officeBuildingService.queryOfficeBuilding(buildingQueryAnalyzer);
            map.put("officeBuildings",officeBuildingsList);
            freemarkerHtmlConfiguration.setData(map);
            boolean flag= FreeMarkerViewUtil.createHtml(freemarkerHtmlConfiguration);
            return flag;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }


}
